package ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class RubberBandCircleDrawingApp {
    public static void main(String[] args) {
    	SwingUtilities.invokeLater(new Runnable() {
        
            public void run() {
                JFrame frame = new JFrame("橡皮筋线画圆程序");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(800, 600);
                
                RubberBandDrawingPanel drawingPanel = new RubberBandDrawingPanel();
                frame.add(drawingPanel);
                
                frame.setVisible(true);
            }
        });
        }
}

class RubberBandDrawingPanel extends JPanel {
    private List<Circle> circles;
    private Point startPoint;
    private Point currentPoint;
    private boolean isDrawing;
    
    public RubberBandDrawingPanel() {
        circles = new ArrayList<Circle>();
        isDrawing = false;
        
        // 鼠标按下事件 - 开始画圆
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)) {
                    startPoint = e.getPoint();
                    currentPoint = startPoint;
                    isDrawing = true;
                }
            }
            
            @Override
            public void mouseReleased(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e) && isDrawing) {
                    currentPoint = e.getPoint();
                    int radius = (int) startPoint.distance(currentPoint);
                    circles.add(new Circle(startPoint, radius));
                    isDrawing = false;
                    repaint();
                }
            }
        });
        
        // 鼠标移动事件 - 更新橡皮筋线
        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isDrawing) {
                    currentPoint = e.getPoint();
                    repaint();
                }
            }
        });
        
        // 设置面板属性
        this.setBackground(Color.WHITE);
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 绘制所有已完成的圆
        g2d.setColor(new Color(0, 0, 100, 150)); // 半透明蓝色
        for (Circle circle : circles) {
            drawCircle(g2d, circle.center, circle.radius);
        }
        
        // 绘制当前正在拖动的橡皮筋线和圆
        if (isDrawing && startPoint != null && currentPoint != null) {
            // 绘制橡皮筋线
            g2d.setColor(Color.RED);
            g2d.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
            
            // 计算半径
            int radius = (int) startPoint.distance(currentPoint);
            
            // 绘制预览圆
            g2d.setColor(new Color(255, 0, 0, 100)); // 半透明红色
            drawCircle(g2d, startPoint, radius);
        }
    }
    
    private void drawCircle(Graphics2D g2d, Point center, int radius) {
        g2d.fillOval(center.x - radius, center.y - radius, radius * 2, radius * 2);
    }
    
    // 内部类表示圆
    private static class Circle {
        Point center;
        int radius;
        
        Circle(Point center, int radius) {
            this.center = center;
            this.radius = radius;
        }
    }

	public void setShapeMode(ShapeMode square) {
		// TODO Auto-generated method stub
		
	}

	public void saveShapesToFile(File file) {
		// TODO Auto-generated method stub
		
	}

	public void loadShapesFromFile(File file) {
		// TODO Auto-generated method stub
		
	}
}